package org.top10.parser;

import java.util.ArrayList;
import java.util.List;


import org.top10.collector.Collector;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class JoyTask extends Task {

	private static final int webSiteID = TOP10Global.WEB_SITE_ID_JOY;// 对应网站ID
	private static final String ENCoding = "gb2312";// 对应网站ID

	public JoyTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {

		PARSER.add(new JoyJokesParser());
		PARSER.add(new JoyKillTimeParser());
		PARSER.add(new JoyAbroadParser());
		PARSER.add(new JoyMotionParser());
		PARSER.add(new JoyFunnyParser());
	}

	public static void main(String args[]) throws Exception {
		String url = "http://joke.joy.cn/applications/surging/index.jsp";
		ParseSource source = Collector.createCache(url, ENCoding);
		new JoyMotionParser().parse(source);
	}
}

abstract class JoyParser extends TOPParserImpl {

	public JoyParser(String c, int t, String u) {
		super(c, t, u);
	}

	public abstract List parse(ParseSource doc) throws Exception;

	public abstract Video build(Node node, int rank) throws Exception;

	// XUNlei所有解析器的公有方法放在这里..
	

	protected Video buildVideo(Node dl, int rank) {
		Video v = new Video();
		v.setRank(rank);
		List<Node> as = new ArrayList<Node>();
		getAllTagsByParent(dl, "a", as);
		Node a = as.get(0);
		String url = getAttrValueByTag(a, "href");
		v.setPlayUrl(url);
		a = as.get(1);
		v.setName(a.getTextContent());
		return v;
	}

	protected List joyBuild(ParseSource source, String sign) throws Exception {
		Node body = source.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("img", "src",sign, body);
		if (node != null) {
			Node parent = node.getParentNode().getParentNode();
			ArrayList<Node> dls = new ArrayList<Node>();
			getAllTagsByParent(parent, "dl", dls);
			List<Video> list = new ArrayList<Video>();
			int rank = 1;
			for (Node d : dls) {
				Video v = build(d, rank);
				System.out.println(v.toString());
				list.add(v);
			}
			return list;
		}
		return null;
	}
}

class JoyJokesParser extends JoyParser {

	private static final String SIGN = "list10030201.jpg";
	private static final String URL="http://joke.joy.cn/applications/surging/index.jsp";
	private static final String TITLE="恶搞雷人榜";
	public JoyJokesParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return joyBuild(doc, SIGN);
	}
}

class JoyKillTimeParser extends JoyParser {
	private static final String URL="http://joke.joy.cn/applications/surging/index.jsp";
	private static final String TITLE="碎片病毒榜";
	
	private static final String SIGN = "list10030202.jpg";

	public JoyKillTimeParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return joyBuild(doc, SIGN);
	}
}

class JoyAbroadParser extends JoyParser {

	private static final String SIGN = "list10030203.jpg";
	private static final String URL="http://joke.joy.cn/applications/surging/index.jsp";
	private static final String TITLE="海外精选榜";
	public JoyAbroadParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return joyBuild(doc, SIGN);
	}
}

class JoyMotionParser extends JoyParser {
	private static final String URL="http://joke.joy.cn/applications/surging/index.jsp";
	private static final String TITLE="动作网剧榜";
	private static final String SIGN = "100587711.jpg";

	public JoyMotionParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return joyBuild(doc, SIGN);
	}
}

class JoyFunnyParser extends JoyParser {
	private static final String URL="http://joke.joy.cn/applications/surging/index.jsp";
	private static final String TITLE="搞笑网剧榜";
	private static final String SIGN = "list10030206.jpg";

	public JoyFunnyParser() {
		super(TITLE, TOP10Global.TYPE_Television,URL);
	}

	public Video build(Node node, int rank) throws Exception {
		return buildVideo(node, rank);
	}

	public List parse(ParseSource doc) throws Exception {
		return joyBuild(doc, SIGN);
	}
}